<?
$picMaxSize = "298*172";
$picMinSize = "529*594";
$action = new Action();
class Action
{
	function Action()
	{
		global $gAct,$gOp;
		switch($gOp)
		{
			case "shwListGame":
				$this->shwListGame();
				break;
 
		    case "ajaxSaveType":
		        $this->ajaxSaveType();
		        break;

		    case "ajaxSaveMedia":
		        $this->ajaxSaveMedia();
		        break;
		    case "ajaxDelWineType":
		        $this->ajaxDelWineType();
		        break;
			case "saveRank":
			    $this->saveRank();
				break;
			default:
				$this->shwListGame();
				break;
		}
	}
	function saveRank()
	{
		global $gSmarty,$gAct,$gOp,$INCOME,$DB,$gCurTime,$GJson;
		if($INCOME['tid']!="" && $INCOME['id'])
		{
		if(substr($INCOME['id'],0,1)=="," || substr($INCOME['id'],strlen($INCOME['id'])-1,1)==",")
		{
			$data = array("st"=>"err","msg"=>"开头第一个  和 末尾 不能是 逗号");
			echo $GJson->encode($data);exit;
		}
		$arrIds = explode(",",$INCOME['id']);
		$newIds = array();
		foreach($arrIds as $v)
		{
			if(strlen($v)>=1 && is_numeric($v))
			{
				$newIds[] = $v;
			}
		}
		if(count($newIds)!=count($arrIds))
		{
			$data = array("st"=>"err","msg"=>"id 有的不正确");
			echo $GJson->encode($data);exit;
		}
		$sql="select * from type where id in(".$INCOME['id'].") and parId=".$INCOME['tid'];
		
		$DB->db_query($sql);
		//echo $DB->db_fetch_num();echo count($arrIds);
		if($DB->db_fetch_num()==count($arrIds))
		{
			$i = 0;
			foreach($arrIds as $v)
			{
				$DB->db_query("update type set rank=".$i." where id=".$v);
				$i++;
			}
			$data = array("st"=>"ok","msg"=>"成功排序");
		}
		else
		{
			$data = array("st"=>"err","msg"=>"id 有的不正确");
		}
		}
		else
		{
			$data = array("st"=>"err","msg"=>"缺少参数");
		}
		echo $GJson->encode($data);
	}
	function ajaxDelWineType()
	{
		global $gSmarty,$gAct,$gOp,$INCOME,$DB,$gCurTime,$GJson;
		$id = $INCOME['id'];
		$r = $DB->db_query_first("select * from type where parId = $id");
		if($r)
		{
			$data = array("st"=>"err","msg"=>"该类下面有子类，不允许删除");
		}
		else
		{
			$DB->db_query("delete from type where id=$id");
			$data = array("st"=>"ok","msg"=>"删除成功");
		}
		echo $GJson->encode($data);
	}
  
    function ajaxSaveType()
    {
    	global $gSmarty,$gAct,$gOp,$INCOME,$DB,$gCurTime,$GJson,$CONF,$gWeb;
		$f['name'] = $INCOME['name'];
		 
		$f['parId'] = $INCOME['tid'];
		$id = $INCOME['editId'];
		if($id)
		{
			$sql = "select * from type where name='".$f['name']."' and id!=$id";
			$r = $DB->db_query_first($sql);
			if($r)
			{
				$gWeb->showMsg("该类别已经存在");
				exit;
			}
		}
		else
		{
			$sql = "select * from type where name='".$f['name']."'";
			$r = $DB->db_query_first($sql);
			if($r)
			{
				$gWeb->showMsg("该类别已经存在");
				exit;
			}
		}
		$id = $INCOME['editId'];
		//$f_upload = new upload_file("i",$CONF['upload_path'],$CONF['upload_maxSize'],"picMin",10);          
		//$picMin = $f_upload->do_save();
		//$picMinType = $f_upload->getFileType();
	    //if($picMin=="errorType")
	    //{
		//	$gWeb->showMsg("文件类型错误 请选择jpg/png/gif 图片类型");
		//	exit;
	   // }
		//else if($picMin=="errorNull")
		//{
		//	$gWeb->showMsg("异常错误");
		//	exit;
		//}
		//$f_upload = new upload_file("i",$CONF['upload_path'],$CONF['upload_maxSize'],"picMax",10);          
		//$picMax = $f_upload->do_save();
		//if($picMax=="errorNull")
		//{
		//	$gWeb->showMsg("异常错误");
		//	exit;
		//}
		//$newPicMax = str_replace(".".$picMinType,"max.".$picMinType,$picMin);
		//rename($picMax,$newPicMax);

		//$f['name']=$name;
		//$picMin = str_replace("../","",$picMin);
		//$f['pic']=$picMin;
		//$f['remark']=$INCOME['remark'];
		//$f['authorId']=$_SESSION['adminUser']['id'];
		//$f['makerId']=$makerId;
		//$f['makerName']=$makerName;
		if($id)
		{
			$f['edtTime']=$gCurTime;
			$DB->db_form_update("type",$f,"id=$id");
		}
		else
		{
			$f['addTime']=$gCurTime;
			$f['edtTime']=$gCurTime;
			$DB->db_form_insert("type",$f);
		}
	    $gWeb->showMsg("保存成功");
	    echo "<script>parent.location=parent.location</script>";
    }
	function shwListGame()
	{
		global $gSmarty,$gAct,$gOp,$INCOME,$DB,$gWeb,$picMaxSize,$picMinSize;

		if($INCOME['tid'])
		{
			unset($_SESSION['typeLevel']);
			
			$parRec = $DB->db_query_first("select * from type where id=".$INCOME['tid']);
			$this->getTypeLevel($INCOME['tid']);
			$_SESSION['typeLevel'][] = "Home";
			$curType = implode("&nbsp;&nbsp;->&nbsp;&nbsp;",array_reverse($_SESSION['typeLevel']));
			$curTypeId = $parRec['id'];
			$con = "where parId=".$INCOME['tid'];
		}
		else
		{
			unset($_SESSION['typeLevel']);
			$_SESSION['typeLevel'][] = "Home";
			$curType = "Home";
			$curTypeId =0;
			$con = "where parId=0";
		}
		if($con)
		{
			$con = $con." and isHide=0";
		}
		$rankIds  = array();
		$sql = "SELECT * FROM type $con order by rank";
		$DB->db_query($sql);
		$data = null;
		$idx = 0;
		while($r = $DB->db_fetch_row())
		{
		    $idx++;
		    $data[] = array("disId"=>$idx,"id"=>$r['id'],"name"=>$r['name'],"name_en"=>$r['name_en'],"name_jp"=>$r['name_jp'],"addTime"=>$r['addTime']);	
			$rankIds[] = $r['id'];
		}
		if($data)
		{
		foreach($data as $k)
		{
			$sql = "select count(*) as num from type where parId=".$k['id']." and isHide=0";
			$ar = $DB->db_query_first($sql);
			$k['cnum'] = $ar['num'];
			$newData[]=$k;
			
		}
		}
		$data = $newData;
		$gSmarty->assign("rankIds",implode(",",$rankIds));
		$gSmarty->assign("curTypeId",$curTypeId);
		$gSmarty->assign("curType",$curType);
		$gSmarty->assign("act",$gAct);
		$gSmarty->assign("picMaxSize",$picMaxSize);
		$gSmarty->assign("picMinSize",$picMinSize);
		$gSmarty->assign("data",$data);
		$gSmarty->display("type/list.tpl");
	}
    function getTypeLevel($parId)
	{
		global $gSmarty,$gAct,$gOp,$INCOME,$DB,$gWeb,$picMaxSize,$picMinSize;
		$r = $DB->db_query_first("select * from type where id=".$parId);
		if($r)
		{
			$_SESSION['typeLevel'][] = $r['name'];
			$this->getTypeLevel($r['parId']);
		}
	}
}
?>